package icu.qx.wm.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import icu.qx.common.utils.CodeUtils;
import icu.qx.wm.domain.WmWarehouse;
import icu.qx.wm.exception.WmWarehouseException;
import icu.qx.wm.mapper.WmWarehouseMapper;
import icu.qx.wm.service.IWmWarehouseService;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * &#064;description  针对表【wm_warehouse(仓库)】的数据库操作Service实现
 * &#064;createDate  2024-06-05 17:01:06
 */
@Service
public class WmWarehouseServiceImpl implements IWmWarehouseService {
    private final WmWarehouseMapper wmWarehouseMapper;
    private final CodeUtils codeUtils;

    @Autowired
    public WmWarehouseServiceImpl(WmWarehouseMapper wmWarehouseMapper, CodeUtils codeUtils) {
        this.wmWarehouseMapper = wmWarehouseMapper;
        this.codeUtils = codeUtils;
    }

    /**
     * 查询仓库
     *
     * @param wCode 仓库主键
     * @return 仓库
     */
    @Override
    public WmWarehouse selectWmWarehouseByWCode(String wCode) {
        return wmWarehouseMapper.selectWmWarehouseByWCode(wCode);
    }

    /**
     * 查询仓库列表
     *
     * @param wmWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<WmWarehouse> selectWmWarehouseList(WmWarehouse wmWarehouse) {
        return wmWarehouseMapper.selectWmWarehouseList(wmWarehouse);
    }

    /**
     * 新增仓库
     *
     * @param wmWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertWmWarehouse(WmWarehouse wmWarehouse) {
        String code = codeUtils.generateCode("wm_warehouse");
        wmWarehouse.setwCode(code);

        wmWarehouse.setCreateTime(DateUtils.getNowDate());

        return wmWarehouseMapper.insertWmWarehouse(wmWarehouse);
    }

    /**
     * 修改仓库
     *
     * @param wmWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateWmWarehouse(WmWarehouse wmWarehouse) {
        wmWarehouse.setUpdateTime(DateUtils.getNowDate());
        return wmWarehouseMapper.updateWmWarehouse(wmWarehouse);
    }

    /**
     * 批量删除仓库
     *
     * @param wCodes 需要删除的仓库主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = WmWarehouseException.class)
    public int deleteWmWarehouseByWCodes(String[] wCodes, Integer[] saCount) {
        try {
            for (Integer integer : saCount) {
                if (integer > 0) {
                    throw new WmWarehouseException("当前仓库存在库区");
                }
            }
            return wmWarehouseMapper.deleteWmWarehouseByWCodes(wCodes);
        } catch (Exception e) {
            throw new WmWarehouseException("删除仓库失败: " + e.getMessage(), e);
        }
    }

    /**
     * 删除仓库信息
     *
     * @param wCode 仓库主键
     * @return 结果
     */
    @Override
    public int deleteWmWarehouseByWCode(String wCode) {
        return wmWarehouseMapper.deleteWmWarehouseByWCode(wCode);
    }

    /**
     * 查询仓库列表
     */
    @Override
    public List<WmWarehouse> selectWmWarehouseWithArea(WmWarehouse wmWarehouse) {
        return wmWarehouseMapper.selectWmWarehouseWithArea(wmWarehouse);
    }

    /**
     * 查询仓库编码
     */
    @Override
    public List<Map<String, String>> selectWmWarehouseCode(WarehouseFormVo warehouseFormVo) {
        return wmWarehouseMapper.selectWmWarehouseCode(warehouseFormVo);
    }
}